<template>
  <div class="mainpg">
    <div class="main_spac1" style="padding-bottom: 5px">
      <div class="main_white">
        <div class="boxdq" v-if="is_have_print">
          <el-form ref="form" label-width="150px" size="medium">
            <el-form-item
              v-for="(item, index) in tableData"
              :key="index"
              :label="item.tem_name"
            >
              <el-select
                v-model="form[index].printIndex"
                placeholder="请选择打印机"
              >
                <el-option label="默认打印机" :value="parseInt(-1)"></el-option>
                <el-option
                  v-for="(printList, index) in printList"
                  :key="index"
                  :label="printList.name"
                  :value="printList.id"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="save">保存</el-button>
            </el-form-item>
          </el-form>
        </div>
        <div class="no_print" v-else>未检测到安装插件</div>
      </div>
    </div>
  </div>
</template>

<script>
import { getLodop } from "@/utils/LodopFuncs";
import { set_print_mark, get_print_mark } from "@/api/global/globalPrint";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";
import { mapGetters } from "vuex";
export default {
  props: {
    tableData: {
      type: Array,
      default: []
    }
  },
  data() {
    return {
      form: [],
      //   打印机
      printList: [],
      //   模板列表
      templateList: {},
      loading: true,
      is_have_print: true,
      print_setting_list: []
    };
  },
  computed: {
    ...mapGetters(["avatar"]),
    print_setting() {
      return `print_setting_list_${this.avatar.company_id}`;
    }
  },
  mounted() {},
  methods: {
    getPrintList() {
      let LODOP = getLodop();
      if (!LODOP) {
        this.is_have_print = false;
        return;
      }
      var iPrinterCount = LODOP.GET_PRINTER_COUNT();
      for (let i = 0; i < iPrinterCount; i++) {
        let item = {};
        let name = LODOP.GET_PRINTER_NAME(i);
        item.id = i;
        item.name = name;
        this.printList.push(item);
      }
      console.log(this.printList);
    },
    // 获取配置信息
    get_print_info() {
      let data = Object.assign(
        apiUrl().name
      );
      localStorage.setItem(
        this.print_setting,
        '[{"id":19,"type":9,"print_index":-1,"company_id":5},{"id":20,"type":10,"print_index":-1,"company_id":5},{"id":21,"type":11,"print_index":-1,"company_id":5},{"id":22,"type":12,"print_index":-1,"company_id":5},{"id":23,"type":13,"print_index":-1,"company_id":5},{"id":24,"type":14,"print_index":-1,"company_id":5},{"id":25,"type":15,"print_index":-1,"company_id":5},{"id":26,"type":17,"print_index":-1,"company_id":5},{"id":27,"type":18,"print_index":-1,"company_id":5}]'
      );

      let list = JSON.parse(localStorage.getItem(this.print_setting));

      list.map(i => {
        this.form.map(x => {
          if (i.type == x.tem_id) {
            let flag = false;
            this.printList.map(e => {
              if (e.id == i.print_index) {
                flag = true;
              }
            });
            if (flag) {
              x.printIndex = i.print_index;
            } else {
              x.printIndex = -1;
            }
          }
        });
      });
    },
    // 处理缓存在本地的打印机配置信息
    handleLocalPrintInfo() {
      let print_setting_list = localStorage.getItem(this.print_setting);
      if (print_setting_list) {
        let list = JSON.parse(print_setting_list);
        this.print_setting_list = list;
        list.map(i => {
          this.form.map(x => {
            if (i.type == x.tem_id) {
              let flag = false;
              this.printList.map(e => {
                if (e.id == i.print_index) {
                  flag = true;
                }
              });
              if (flag) {
                x.printIndex = i.print_index;
              } else {
                x.printIndex = -1;
              }
            }
          });
        });
      } else {
      }

      this.loading = false;
    },
    save() {
      let form = JSON.stringify(this.form);
      let data = Object.assign(
        apiUrl().name,
        { data: form }
      );

      let print_setting_list = localStorage.getItem(this.print_setting);

      if (print_setting_list) {
        let list = JSON.parse(print_setting_list);
        this.print_setting_list = list;
      }

      this.print_setting_list.map(e => {
        this.form.map(i => {
          if (e.type == i.tem_id) {
            e.print_index = i.printIndex;
          }
        });
      });

      localStorage.setItem(
        this.print_setting,
        JSON.stringify(this.print_setting_list)
      );
      this.$message.success("保存成功");
      return;
    }
  },
  created() {
    this.loading = true;
    // 获取打印机列表
    this.getPrintList();
    let arr = [];
    this.tableData.map(item => {
      let obj = {
        tem_id: item.tem_id,
        printIndex: -1
      };
      arr.push(obj);
    });
    this.form = arr;
    let print_setting_list = localStorage.getItem(this.print_setting);
    if (print_setting_list) {
      this.handleLocalPrintInfo();
    } else {
      this.get_print_info();
    }
  }
};
</script>

<style lang="scss" scoped>
.main_spac1 {
  padding-top: 0;
}
/deep/ .el-select {
  width: 260px;
}
/deep/ .el-message-box__title {
  font-size: 16px;
}
</style>
